package org.spolo.android.nodetree;

import android.graphics.Canvas;
import android.graphics.RectF;
import android.graphics.Region;
import android.view.View;

public class Stage extends Vcanvas {
	private static Stage instance;
	private Region region = new Region();
	private boolean m_refreshposted = false;

	public void AddDirtyRect(RectF r) {
		region.op((int) r.left, (int) r.top, (int) r.right, (int) r.bottom,
				Region.Op.UNION);
		if (!m_refreshposted) {
			if (this.getView() != null) {
				this.getView().postInvalidate();
				m_refreshposted = true;
			}
		}
	}

	public boolean DirtyContainRect(RectF r) {
		boolean b = region.op((int) r.left, (int) r.top, (int) r.right,
				(int) r.bottom, Region.Op.UNION);
		return b;
	}

	protected void ClearDirty() {
		region.setEmpty();
		m_refreshposted = false;
	}

	private Stage() {
		super();
		m_parent = this;
		setDirtyRect(0, 0, 0, 0);
	}

	public static Stage getInstance() {
		if (instance != null) {
			return instance;
		} else {
			return instance = new Stage();
		}
	}

	public void injectMessage(int type, Object me) {
		this.recuriHandleEvent(type, me);

	}

	/**
	 * 请求重绘整棵场景树
	 * 
	 * @param canvas
	 */
	public void drawSceneTree(Canvas canvas) {
		// 如果脏区为空
		// if (!region.isEmpty())
		// canvas.clipRegion(region);
		this.drawSelf(canvas, 0f, 0f);
		this.ClearDirty();
	}

	/**
	 * 获取View对象
	 * 
	 * @return
	 */
	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

}
